package ch00.ch02;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @Author lzp
 * @Date 2024-04-02
 */
public class Test {

    public static int he(int n) throws Exception {

        Sum sum = new Sum();
        Class<? extends Sum> aClass = sum.getClass();
        Method method = aClass.getMethod("getSum", int.class);
        Parameter[] parameters = method.getParameters();
        Parameter parameter = parameters[0];

        if (parameter.isAnnotationPresent(Max.class)){

            //获取注解的值
            int value = parameter.getAnnotation(Max.class).value();
            if(n > value){
                throw new RuntimeException("对不起，我只能最多处理 1-" + value + "之间的和！");
            }
        }
        int h = Sum.getSum(n);
        return h;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(he(100));
        System.out.println(he(1000));
    }
}
